'use strict';
//https://www.cnblogs.com/honkerzh/p/13994598.html
// 处理路径的模块
const path = require('path');
const fs = require('fs');
const getPublicUrlOrPath = require('react-dev-utils              /getPublicUrlOrPath');

//项目根目录 
const appDirectory = fs.realpathSync(process.cwd());
//生成绝对路径
//path.resolve()理解 https://blog.csdn.net/kikyou_csdn/article/details/83150538 
const resolveApp = relativePath => path.resolve(appDirectory,relativePath);

// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
//所有资源的公共防卫 ：
const getPublicUrlOrPath = getPublicUrlOrPath(
    process.env.NODE_ENV === 'development',
    require(resolveApp('package.json')).homepage,
    process.env.PUBLIC_URL
)

const buildPath = process.env.BUILD_PATH || 'build';
// 扩展名
const moduleFileExtensions = [
    'web.mjs',
    'mjs',
    'web.js',
    'js',
    'web.ts',
    'ts',
    'web.tsx',
    'tsx',
    'json',
    'web.jsx',
    'jsx',
];

// 解析模块的方法
// Resolve file paths in the same order as webpack
//fs.existsSync方法的使用说明
//以同步的方法检测目录是否存在。如果目录存在 返回 true ，如果目录不存在 返回false
const resolveModule  = ( resolveFn , filePath )=>{
    const extension = moduleFileExtensions.find(extension=>
        fs.existsSync(resolveFn(`${filePath}.${extension}`))
    );
    
    if(extension){
        return resolveFn(`${filePath}.${extension}`)
    }

    return resolveFn(`${filePath}.js`);
}

// 获取文件
// config after eject: we're in ./config/
module.exports = {
    dotenv: resolveApp('.env'),
    appPath: resolveApp('.'),
    appBuild: resolveApp(buildPath),
    appPublic: resolveApp('public'),
    appHtml: resolveApp('public/index.html'),
    appIndexJs: resolveModule(resolveApp, 'src/index'),
    appPackageJson: resolveApp('package.json'),
    appSrc: resolveApp('src'),
    appTsConfig: resolveApp('tsconfig.json'),
    appJsConfig: resolveApp('jsconfig.json'),
    yarnLockFile: resolveApp('yarn.lock'),
    testsSetup: resolveModule(resolveApp, 'src/setupTests'),
    proxySetup: resolveApp('src/setupProxy.js'),
    appNodeModules: resolveApp('node_modules'),
    appWebpackCache: resolveApp('node_modules/.cache'),
    appTsBuildInfoFile: resolveApp('node_modules/.cache/tsconfig.tsbuildinfo'),
    swSrc: resolveModule(resolveApp, 'src/service-worker'),
    publicUrlOrPath,
  };
  
  
  module.exports.moduleFileExtensions = moduleFileExtensions;